package com.fenghuajueli.libbasecoreui.utils;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.blankj.utilcode.util.LogUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.disklrucache.DiskLruCache;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.Key;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.engine.cache.DiskCache;
import com.bumptech.glide.load.engine.cache.SafeKeyGenerator;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.target.Target;
import com.bumptech.glide.request.transition.Transition;
import com.bumptech.glide.signature.EmptySignature;
import com.fenghuajueli.libbasecoreui.BaseApplication;
import com.fenghuajueli.libbasecoreui.R;

import java.io.File;
import java.io.IOException;

/**
 * 图片加载工具封装
 */
public class ImageLoadManager {

    public static void loadForImageView(ImageView imageView, String url) {
        Glide.with(imageView.getContext())
                .load(new GlideUrlWrapper(url).getUrl())
                .apply(initRequestOptions())
                .into(imageView);
    }

    public static void loadCacheForImageView(ImageView imageView, String url) {
        Glide.with(imageView.getContext())
                .load(new GlideUrlWrapper(url).getUrl())
                .apply(getCacheOptions())
                .into(imageView);
    }

    public static void loadForImageViewNoProxy(ImageView imageView, String url) {
        Glide.with(imageView.getContext())
                .load(url)
                .apply(initRequestOptions())
                .into(imageView);
    }

    public static void loadForImageViewNoProxy(ImageView imageView, Uri uri) {
        Glide.with(imageView.getContext())
                .load(uri)
                .apply(initRequestOptions())
                .into(imageView);
    }

    public static void loadForImageView(Context context, ImageView imageView, String url) {
        Glide.with(context)
                .load(new GlideUrlWrapper(url).getUrl())
                .apply(initRequestOptions())
                .into(imageView);
    }

    public static void loadForImageView(ImageView imageView, int resId) {
        Glide.with(imageView.getContext())
                .load(resId)
                .apply(initRequestOptions())
                .into(imageView);
    }

    private static RequestOptions requestOptions;

    private static RequestOptions initRequestOptions() {
        if (requestOptions == null) {
            requestOptions = new RequestOptions()
                    .error(R.drawable.shape_img_load_error_drawable)
                    .placeholder(R.drawable.shape_img_load_error_drawable)
                    .diskCacheStrategy(DiskCacheStrategy.RESOURCE);
        }
        return requestOptions;
    }


    private static RequestOptions cacheOptions;

    private static RequestOptions getCacheOptions() {
        if (cacheOptions == null) {
            cacheOptions = new RequestOptions()
                    .onlyRetrieveFromCache(true)
                    .placeholder(R.drawable.shape_img_load_error_drawable)
                    .error(R.drawable.shape_img_load_error_drawable);

        }
        return cacheOptions;
    }


    public static void loadForViewGroup(final View view, String url) {
        Glide.with(view.getContext())
                .load(url)
                .apply(initRequestOptions())
                .into(new CustomTarget<Drawable>() {
                    @Override
                    public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                        view.setBackground(resource);
                    }

                    @Override
                    public void onLoadCleared(@Nullable Drawable placeholder) {

                    }
                });
    }

    public static void loadForViewGroup(final View view, int resId) {
        Glide.with(view.getContext())
                .load(resId)
                .apply(initRequestOptions())
                .into(new CustomTarget<Drawable>() {
                    @Override
                    public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                        view.setBackground(resource);
                    }

                    @Override
                    public void onLoadCleared(@Nullable Drawable placeholder) {

                    }
                });
    }

    public static void loadForViewGroup(Context context, final View view, int resId) {
        Glide.with(context)
                .load(resId)
                .apply(initRequestOptions())
                .into(new CustomTarget<Drawable>() {
                    @Override
                    public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                        view.setBackground(resource);
                    }

                    @Override
                    public void onLoadCleared(@Nullable Drawable placeholder) {

                    }
                });
    }


}
